layui.use(['element','form','table'],function(){
    var table = layui.table;
    var form = layui.form;

    table.render({
        elem: '#demo'
        ,toolbar: '#toolbarDemo'
        ,url: '/authUser' //数据接口
        ,page: true //开启分页
        ,cols: [[ //表头
            {field: 'id',title: '人员编号', sort: true, width:120}
            ,{field: 'name', title: '帐号名'}
            ,{field: 'userTrueName',edit: 'text', title: '人员姓名', width:100}
            ,{field: 'userPhone', edit: 'text',title: '电话号码'}
            ,{field: 'userEmail', edit: 'text',title: '邮箱', width:200}
            ,{field: 'userDepartmentName', title: '隶属部门'}
            ,{field: 'userGroupName', title: '隶属上司', width:100}
            ,{field: 'roleName', title: '角色',sort: true,}
            ,{fixed: 'right',align:'center', toolbar: '#barDemo',title: '操作',}
        ]]
    });

    //工具栏监听事件--删除
    table.on('tool(test)', function(obj){
        var data = obj.data;
        var layEvent = obj.event;
        var id = obj.data.id;
        if(layEvent === 'del'){
            layer.confirm('真的删除行么', function(index){
                obj.del();
                layer.close(index);
                console.log(data)
                //向服务端发送删除指令
                $.ajax({
                    url: '/authUser/'+id,
                    type: 'DELETE',
                    dataType: 'json',
                    error: function (xhr, ajaxOptions, thrownError) {
                        if(xhr.status==403) {
                            alert("无权限")
                            location.reload([true])
                        }
                    }
                })
                    .done(function (result) {
                        if (result.code == 500) {
                            layer.msg('删除失败', {
                                icon: 1
                            })
                            location.reload([true])
                        }else{
                            layer.msg('删除成功', {
                                icon: 1
                            })
                        }
                    });
            });
        }else if(layEvent === 'edit'){
            window.location.href="userEdit.html?#"+id;
        }
    });

    //监听单元格编辑--编辑
    table.on('edit(test)', function (obj) {
        var value = obj.value //得到修改后的值
            , data = obj.data //得到所在行所有键值
            , field = obj.field; //得到字段
        var a = {
            'userId': data.id,
            'userName': data.name,
            'userTrueName': data.userTrueName,
            'userPhone':data.userPhone,
            'userEmail':data.userEmail
        };
        var data = JSON.stringify(a)
        $.ajax({
            url: '/authUser',
            type: 'PUT',
            headers: { 'Content-Type': 'application/json;charset=utf-8' },
            dataType: 'json',
            data: data,
            error: function (xhr, ajaxOptions, thrownError) {
                if(xhr.status==403) {
                    alert("无权限")
                    location.reload([true])
                }
            }
        })
            .done(function (result) {
                if (result.code == 500) {
                    layer.msg('修改失败', {
                        icon: 1
                    })
                    location.reload([true])
                }else{
                    alert("修改成功")
                    location.reload([true])
                }
            });
    });

    //头部工具栏监听事件--增加
    table.on('toolbar(test)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'add':
                window.location.href="userAdd.html"
                break;
        };
    });

});